package com.mycase.im.boot.controller;

import com.mycase.im.boot.entity.database.ImFriendRequest;
import com.mycase.im.boot.entity.database.ImUser;
import com.mycase.im.boot.service.FriendRequestService;
import com.mycase.im.boot.service.UserFriendService;
import com.mycase.im.boot.service.UserService;
import com.mycase.im.boot.entity.communication.ResponseMessage;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

import java.util.List;

import static com.mycase.im.boot.entity.communication.ResponseMessage.*;

/**
 * @Author ZhuHJay
 * @Date 2021/8/17 15:31
 */
@RestController
@RequestMapping("/api/apply")
public class FriendRequestController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserFriendService userFriendService;
    @Autowired
    private FriendRequestService friendRequestService;

    @PostMapping("/submit")
    public ResponseMessage submitApply(@Valid ImFriendRequest imFriendRequest){

        // id不能相同, 相同就是同一个人
        if( imFriendRequest.getReqId().equals(imFriendRequest.getRespId()) ){
            return error("不能添加自己为好友");
        }

        // 先查询 这两个id 是否存在
        Boolean isExistReq = userService.isExist(imFriendRequest.getReqId());
        Boolean isExistResp = userService.isExist(imFriendRequest.getRespId());

        if( !isExistReq || !isExistResp ){
            return error("提交信息有误, 请重试");
        }

        // 先查询 两人之间是否是 好友关系
        Boolean isFriend = userFriendService
                .isFriendRelation(
                        imFriendRequest.getReqId(),
                        imFriendRequest.getRespId());

        if( isFriend ){
            return error("用户之间已经是好友关系");
        }

        // 上面都不是, 那么就开始 保存申请
        friendRequestService.saveApply(imFriendRequest);

        return success();
    }

    @GetMapping("/list")
    public ResponseMessage getApplyList(
            @NotBlank(message = "请求失败, 请重试") String id){

        // 获取该用户的信息
        ImUser userInfo = userService.getUserInfoById(id);

        // 查询该账号的好友申请(该账号发出)
        List<ImFriendRequest> myApplyList = friendRequestService.getMyApply(id);
        // 获取列表中的用户的信息
        myApplyList.forEach(item -> {
            // 修改请求人的用户信息
            item.setReqInfo(userInfo);
            // 获取接收人的用户信息
            ImUser respUserInfo = userService.getUserInfoById(item.getRespId());
            item.setRespInfo(respUserInfo);
        });

        // 查询该账号的好友申请(接收申请)
        List<ImFriendRequest> otherApplyList = friendRequestService.getOtherApply(id);
        // 获取列表中的用户的信息
        otherApplyList.forEach(item -> {
            // 修改请求人的用户信息
            ImUser reqUserInfo = userService.getUserInfoById(item.getReqId());
            item.setReqInfo(reqUserInfo);
            // 获取接收人的用户信息
            item.setRespInfo(userInfo);
        });

        return success().appendData("myApply", myApplyList)
                        .appendData("otherApply", otherApplyList);
    }

    @PostMapping("/reply")
    public ResponseMessage replyApply(
            @Min(value = 1, message = "参数错误, 请重试") Integer tId,
            @Range(max = 2, min = 1, message = "参数错误, 请重试") Integer status){

        friendRequestService.replyApply(tId, status);

        return success();
    }

}
